package com.kerbores.ad.entries;

import org.nutz.dao.entity.annotation.Column;
import org.nutz.lang.Strings;
import org.nutz.lang.Times;

/**
 * @author 王贵源
 *
 *         create at 2014年9月25日 下午3:26:43
 */
public class ApplicationServer extends AuthzResource {
	/**
	 * 资源ip
	 */
	@Column("res_ip")
	private String ip;
	/**
	 * 资源端口
	 */
	@Column("res_port")
	private int port;
	/**
	 * 应用服务器中应用部署路径
	 */
	@Column("res_app_path")
	private String appPath;
	/**
	 * 自定义启动路径
	 */
	@Column("res_cus_start_cmd")
	private String customerStartCommond;

	/**
	 * 
	 */
	public ApplicationServer() {
		setType(RESOURCE_TYPE.APPLICATIONSERVER);
	}

	/**
	 * 启动一台应用服务器
	 */
	@Override
	public boolean start() {
		if (Strings.isBlank(customerStartCommond)) {
			System.err.println("Starting Server " + getType() + " at" + Times.now());
			return true;
		}
		return runingSSHCMD(ip, port, customerStartCommond);
	}

	/**
	 * 执行ssh命令
	 * 
	 * @param ip
	 *            地址
	 * @param port
	 *            端口
	 * @param customerStartCommond
	 *            命令
	 */
	private boolean runingSSHCMD(String ip, int port, String customerStartCommond) {
		return false;
	}

	/**
	 * 创建一台tomcat服务器
	 * 
	 * @return
	 */
	public static ApplicationServer tomcat() {
		ApplicationServer server = new ApplicationServer();
		server.setType(RESOURCE_TYPE.TOMCAT);
		return server;
	}

	/**
	 * 创建一台jetty服务器
	 * 
	 * @return
	 */
	public static ApplicationServer jetty() {
		ApplicationServer jetty = new ApplicationServer();
		jetty.setType(RESOURCE_TYPE.JETTY);
		return jetty;
	}

	/**
	 * @return the ip
	 */
	public String getIp() {
		return ip;
	}

	/**
	 * @param ip
	 *            the ip to set
	 */
	public ApplicationServer setIp(String ip) {
		this.ip = ip;
		return this;
	}

	/**
	 * @return the port
	 */
	public int getPort() {
		return port;
	}

	/**
	 * @param port
	 *            the port to set
	 */
	public ApplicationServer setPort(int port) {
		this.port = port;
		return this;
	}

	/**
	 * @return the appPath
	 */
	public String getAppPath() {
		return appPath;
	}

	/**
	 * @param appPath
	 *            the appPath to set
	 */
	public ApplicationServer setAppPath(String appPath) {
		this.appPath = appPath;
		return this;
	}

	/**
	 * @return the customerStartCommond
	 */
	public String getCustomerStartCommond() {
		return customerStartCommond;
	}

	/**
	 * @param customerStartCommond
	 *            the customerStartCommond to set
	 */
	public ApplicationServer setCustomerStartCommond(String customerStartCommond) {
		this.customerStartCommond = customerStartCommond;
		return this;
	}

}
